/**
 * 工作流节点类型配置
 */

export const NODE_TYPES = {
  CREATE_EXPERIMENT: 'create_experiment',
  DOWNLOAD_DATA: 'download_data',
  DATA_PROCESSING: 'data_processing',
  CONDITION_BRANCH: 'condition_branch'
}

export const NODE_CONFIGS = [
  {
    type: NODE_TYPES.CREATE_EXPERIMENT,
    label: '创建实验',
    icon: 'Coin',
    color: '#409EFF',
    description: '基于实验模板创建并启动实验实例',
    defaultConfig: {
      template_id: '',
      experiment_name: '',
      polling_interval: 30 // 默认30分钟轮询一次
    },
    configFields: [
      {
        key: 'experiment_name',
        label: '实验名称',
        type: 'input',
        required: true,
        placeholder: '实验名称'
      },
      {
        key: 'template_id',
        label: '实验模板',
        type: 'select',
        required: true,
        placeholder: '请选择实验模板'
      }
      // {
      //   key: 'polling_interval',
      //   label: '轮询间隔(分钟)',
      //   type: 'select',
      //   required: true,
      //   options: [
      //     { label: '30分钟', value: 30 },
      //     { label: '1小时', value: 60 },
      //     { label: '2小时', value: 120 },
      //     { label: '3小时', value: 180 },
      //     { label: '6小时', value: 360 }
      //   ]
      // }
    ]
  },
  {
    type: NODE_TYPES.DOWNLOAD_DATA,
    label: '下载数据',
    icon: 'Download',
    color: '#67C23A',
    description: '从指定数据源下载数据到目标位置',
    defaultConfig: {
      source_type: 'OBS',
      source_path: '',
      target_path: '',
      download_strategy: 'overwrite'
    },
    configFields: [
      {
        key: 'source_type',
        label: '数据源类型',
        type: 'select',
        required: true,
        options: [
          { label: 'OBS', value: 'OBS' },
          { label: 'HTTP', value: 'HTTP' },
          { label: 'FTP', value: 'FTP' }
        ]
      },
      {
        key: 'source_path',
        label: '源路径',
        type: 'input',
        required: true,
        placeholder: '请输入数据源路径'
      },
      {
        key: 'target_path',
        label: '目标路径',
        type: 'input',
        required: true,
        placeholder: '请输入目标存储路径'
      },
      {
        key: 'download_strategy',
        label: '下载策略',
        type: 'select',
        required: true,
        options: [
          { label: '覆盖', value: 'overwrite' },
          { label: '跳过', value: 'skip' },
          { label: '增量', value: 'incremental' }
        ]
      }
    ]
  },
  {
    type: NODE_TYPES.DATA_PROCESSING,
    label: '数据处理',
    icon: 'Operation',
    color: '#E6A23C',
    description: '调用平台预定义的API进行数据处理',
    defaultConfig: {
      api_name: '',
      request_params: {}
    },
    configFields: [
      {
        key: 'api_name',
        label: 'API名称',
        type: 'select',
        required: true,
        placeholder: '请选择API',
        options: [
          { label: '数据清洗API', value: 'data_clean' },
          { label: '数据转换API', value: 'data_transform' },
          { label: '数据增强API', value: 'data_augment' }
        ]
      },
      {
        key: 'request_params',
        label: '请求参数',
        type: 'json',
        required: false,
        placeholder: '请输入API请求参数（JSON格式）'
      }
    ]
  },
  {
    type: NODE_TYPES.CONDITION_BRANCH,
    label: '条件分支',
    icon: 'Fork',
    color: '#F56C6C',
    description: '根据条件判断选择执行路径',
    defaultConfig: {
      conditions: [
        {
          condition: '',
          goto: ''
        }
      ]
    },
    configFields: [
      {
        key: 'conditions',
        label: '条件配置',
        type: 'conditions',
        required: true,
        description: '支持多个条件分支，按顺序匹配'
      }
    ]
  }
]

// 获取节点配置
export const getNodeConfig = (type) => {
  return NODE_CONFIGS.find(config => config.type === type) || null
}

// 获取所有节点类型
export const getAllNodeTypes = () => {
  return NODE_CONFIGS.map(config => ({
    type: config.type,
    label: config.label,
    icon: config.icon,
    color: config.color,
    description: config.description
  }))
}

// 创建新节点
export const createNewNode = (type, position = { x: 0, y: 0 }) => {
  const config = getNodeConfig(type)
  if (!config) {
    throw new Error(`Unknown node type: ${type}`)
  }

  return {
    id: `node_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`,
    type: type,
    position: position,
    data: {
      label: config.label,
      config: { ...config.defaultConfig }
    }
  }
}

// 校验节点配置
export const validateNodeConfig = (node) => {
  const config = getNodeConfig(node.type)
  if (!config) {
    return { valid: false, errors: ['未知的节点类型'] }
  }

  const errors = []
  config.configFields.forEach(field => {
    if (field.required && !node.data.config[field.key]) {
      errors.push(`${field.label}不能为空`)
    }
  })

  return {
    valid: errors.length === 0,
    errors
  }
}
